package com.filters;

import com.pojo.Users;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/reception/*")
public class ReceptionFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String uri = request.getRequestURI();
        Users users = (Users) request.getSession().getAttribute("reception");
        if (users==null&&!uri.contains("/login.jsp")&&!uri.contains("/login")&&!uri.contains("/css")&&!uri.contains("/js")&&!uri.contains("/fonts")){
                response.sendRedirect("login.jsp");
        }else {
            if(users!=null){
                if (users.getType()==0){
                    request.setAttribute("msg","后台用户，不可登录前台");
                    response.sendRedirect("login.jsp");
                }
            }
            chain.doFilter(request, response);
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
